<%@ include file="shared_header.jsp" %>
<%@ page import="vrp.Partner" %>
<%@ page import="vrp.Vehicle" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
	
    query = "select from " + Partner.class.getName() + " where type==1 order by name asc";
    List<Partner> partners = (List<Partner>) pm.newQuery(query).execute();

    String[] vehicleStatusValue = {Integer.toString(0),Integer.toString(1)};
	String[] vehicleStatusDisplay = {"Ready","Unavailable"};

	List<Vehicle> vehicles;	
	
	String partnerIDStr = request.getParameter("carrier");
	long partnerID = 0;
	if (partnerIDStr != null && !partnerIDStr.equals("") && !partnerIDStr.equals("0")) {
		partnerID = Integer.parseInt(partnerIDStr);
		Key partnerKey = KeyFactory.createKey(Partner.class.getSimpleName(), partnerID);
		query = "select from " + Vehicle.class.getName() + " where partner == partnerParam parameters String partnerParam order by name asc";
		vehicles = (List<Vehicle>) pm.newQuery(query).execute(partnerKey);
	} else {
	    query = "select from " + Vehicle.class.getName() + " order by name asc";
	    vehicles = (List<Vehicle>) pm.newQuery(query).execute();
	}	
%>
	<div style="padding: 0px 10px;">
		<h2>List of Vehicles</h2>
		<div>
			<form target="_self" action="/vehicles.jsp">
				Carrier:&nbsp;
    			<select name="carrier">
    				<option value="">All Carriers</option>
<%
    				for (Partner p : partners) {
%>			    			
				 	<option value="<%= p.getKey().getId() %>" <%= ((partnerID == p.getKey().getId())?"selected":"") %>><%= p.getName() %></option>
<%
       			}
%>							
				</select>
				<input type="Submit" value="Select" />
	    	</form>
		</div>		
		<br/>	
		<table style="width: 100%;">
<%
    if (vehicles.isEmpty()) {
%>
			<tr><td>There is no vehicle in the database.</td></tr>
<%
    } else {
%>
			<tr>
				<th>Name</th>
				<th>Capacity</th>
				<th>Operating Cost</th>
				<th>Vehicle Status</th>
				<th>Action</th>
			</tr>
<%
        for (Vehicle p : vehicles) {
%>
			<tr>
				<td style="text-align: left;"><%= p.getName() %></td>
				<td style="text-align: right;"><%= p.getCapacity() %></td>
				<td style="text-align: right;"><%= p.getOperatingCost() %></td>
				<td style="text-align: center;"><%= writeSwitch(Integer.toString(p.getVehicleStatus()),vehicleStatusValue,vehicleStatusDisplay) %></td>
				<td style="text-align:center;">
					<input type="button" value="Edit" class="buttonedit"/>
					<input type="button" value="Delete" class="buttondelete"/>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="5">
					<form action="/editvehicle" method="post">
						<div><b>Edit Vehicle</b></div>
			    		<div>Name:&nbsp;<input type="text" name="name" value="<%= p.getName() %>" /></div>
			    		<div>Capacity:&nbsp;<input type="text" name="capacity" value="<%= p.getCapacity() %>" /></div>
			    		<div>Operating Cost:&nbsp;<input type="text" name="operatingCost" value="<%= p.getOperatingCost() %>" /></div>
			    		<div>Vehicle Status:&nbsp;<%= writeSelect("vehicleStatus",Integer.toString(p.getVehicleStatus()),vehicleStatusValue,vehicleStatusDisplay) %></div>
			    		<input type="hidden" name="key" value="<%= p.getKey().getId() %>"/>
						<input type="submit" value="Edit" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="5">
					<form action="/deletevehicle" method="post">
						<div><b>Delete Vehicle</b></div>
			    		<div>This action would delete the above vehicle permanently. Proceed?</div>
						<input type="hidden" name="key" value="<%= p.getKey().getId() %>"/>
						<input type="submit" value="Delete" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
<%
        }
    }
%>
			
		</table>
		<input type="button" value="Add Vehicle" class="buttonadd" />
  		<div style="display: none;">
			<form action="/addvehicle" method="post">
				<div>Carrier:&nbsp;
	    			<select name="partner">
<%
     				for (Partner p : partners) {
%>			    			
					 	<option value="<%= p.getKey().getId() %>" <%= ((partnerID == p.getKey().getId())?"selected":"") %>><%= p.getName() %></option>
<%
        			}
%>							
					</select>
	    		</div>
	    		<div>Name:&nbsp;<input type="text" name="name" /></div>
	    		<div>Capacity:&nbsp;<input type="text" name="capacity" /></div>
	    		<div>Operating Cost:&nbsp;<input type="text" name="operatingCost" /></div>
	    		<div>Vehicle Status:&nbsp;<%= writeSelect("vehicleStatus","",vehicleStatusValue,vehicleStatusDisplay) %></div>
	    		<div><input type="submit" value="Add" /><input type="button" value="Cancel" class="buttoncancel" /></div>
	  		</form>
	  	</div>
	</div>
	
<%
    pm.close();
%>
	
<%@ include file="shared_footer.jsp" %>